package com.zlq.singleton.test;
/*
 *单例模式的懒汉式实现
 *
 *
 */

public class SingletonTest2 {
    public static void main(String[] args) {
        Order order1 = Order.getInstance();
        Order order2 = Order.getInstance();

        System.out.println(order1 == order2);
    }

}


class Order {
    //1.私有化类的构造器
    private Order() {

    }

    //2.声明当前类的对象，没有初始化
    //4.此对象也必须声明为静态的
    private static Order instance = null;

    //3.声明public、static的返回当前类对象的方法
    public static Order getInstance() {
//        //方式一：效率稍差
//        synchronized (Order.class) {
//            if (instance == null) {
//                instance = new Order();
//            }
//            return instance;
//        }
//    }


        //方式二：效率更高
        if (instance == null) {
            synchronized (Order.class) {
                if (instance == null) {
                    instance = new Order();
                }
            }
        }
        return instance;
    }
}